package com.example.com.skills_utf8;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

import com.app.BaseActivity;
import com.app.net.NetHelper;
import com.app.umeng.Umeng;
import com.app.util.Utils;
import com.baidu.location.BDLocation;
import com.umeng.message.PushAgent;
import com.utils.g;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import cn.jpush.android.api.JPushInterface;


public class _62LoginActivity extends BaseActivity {

    _62LoginActivity activity = this;
    Button btn_register;
    Button btn_submit;
    RadioGroup radiogroup;
    int check_id;
    int type;

    PushAgent mPushAgent;


    BDLocation bd;


    private static class MyHandler extends Handler
    {

        private final WeakReference<Activity> link;
        public MyHandler(Activity activity) {
            link = new WeakReference<Activity>(activity);
        }


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            _62LoginActivity activity = (_62LoginActivity)link.get();
            if(activity != null){

                if(msg.what == 0){
                    activity.bd = (BDLocation)msg.obj;

                    ApplicationCustom.getInstance().location.OnStop();

                    g.id_umeng = activity.mPushAgent.getRegistrationId();
                    g.id_jpush = JPushInterface.getRegistrationID(activity);

                    Log.e(activity.TAG, "id_umeng: " + g.id_umeng);
                    Log.e(activity.TAG, "id_jpush: " + g.id_jpush);
                    /*
                    try {
                        activity.mPushAgent.addAlias("xx","xx");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    */
                    Utils.dismissLoadingDialog();
                }
                else{
                    //Utils.showLoadingDialog(activity, (String)msg.obj);

                    activity.ShowToast((String)msg.obj);
                }


            }






        }
    }

    Handler handler = new MyHandler(this);





    @Override
    public Integer InitLayout() {
        return R.layout.activity__62_login;
    }

    @Override
    public void InitView() {

        btn_register = (Button)findViewById(R.id.btn_register);
        radiogroup = (RadioGroup)findViewById(R.id.radiogroup);
        btn_submit = (Button)findViewById(R.id.btn_submit);


    }

    @Override
    public void InitListener() {

    }

    @Override
    public void InitProcess() {

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, _62RegisterActivity.class);
                startActivity(intent);

            }
        });


        check_id = R.id.rb_location;
        radiogroup.check(check_id);

        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                check_id = checkedId;
            }
        });

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.e(TAG, "check_id：" + check_id);


                if (check_id == R.id.rb_voice) {
                    do_voice();
                } else if (check_id == R.id.rb_pic) {
                    do_pic();
                } else if (check_id == R.id.rb_location) {
                    do_location();
                }

            }
        });



        //
        ApplicationCustom.getInstance().location.SetHandler(handler, false);



        // 消息推送申请key的时候要键入包名，推断不同app要使用不同包名
        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.setAppkeyAndSecret(g.umeng_app_key, g.umeng_app_key_secret);
        Umeng.OnCreate_message_init(activity);
        Umeng.OnCreate_message(activity);

        mPushAgent.setPushIntentServiceClass(_62PushIntentService.class);




        Utils.showLoadingDialog(activity, "加载中", false);


        Umeng.OnCreate_analytics_init(this);
        Umeng.OnCreate_analytics(this);

    }

    @Override
    protected void onPause() {
        super.onPause();

        Umeng.OnPause_analytics(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        Umeng.OnResume_analytics(this);
    }



    void do_voice(){
        type = 0;

    }

    void do_pic(){
        type = 1;

    }

    void do_location(){
        type = 2;

        if(bd != null)
            activity.requestNetData_login("" + bd.getLatitude(), "" + bd.getLongitude(), "" + bd.getRadius());


    }



    void requestNetData_login(final String Latitude, final String Longitude, final String Radius){



        new NetHelper(requestQueue){

            final NetHelper nethelper = this;
            int count = 1;

            @Override
            public void UI_init() {
                super.UI_init();

                Utils.showLoadingDialog(activity, "登录中");
            }

            @Override
            public String init(ArrayList<BasicNameValuePair> map) {

                map.add(new BasicNameValuePair("id_umeng", g.id_umeng));
                map.add(new BasicNameValuePair("id_user", g.id_user));
                map.add(new BasicNameValuePair("type", ""+type));
                map.add(new BasicNameValuePair("Latitude", Latitude));
                map.add(new BasicNameValuePair("Longitude", Longitude));
                map.add(new BasicNameValuePair("Radius", Radius));

                return g.SERVER_PAY_HOST + g.SERVER_PAY_API.login;
            }

            @Override
            public void onComplete(String message) throws JSONException {

                JSONObject obj = new JSONObject(message);
                String msg = obj.getString("message");
                int result = obj.getInt("result");

                if(result == 0){
                    JSONObject data = obj.getJSONObject("data");
                    g.token = data.getString("token");
                    Intent intent = new Intent(activity, _62Activity.class);
                    startActivity(intent);
                    finish();

                }
                else{

                }

                ShowToast(msg);


            }

            @Override
            public void OnError(String StatusCode) {
                super.OnError(StatusCode);

                if(count++ < 3){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                        if(!activity.isFinishing())
                            nethelper.start_volley_GET();
                        }
                    }, (long)Math.pow(3, count)*1000);

                }

            }

            @Override
            public void OnFinal() {
                super.OnFinal();

                Utils.dismissLoadingDialog();
            }

        }.start_volley_GET();

    }


}
